package com.zx.base.widget;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

import com.zx.base.util.ActivityTaskManager;

public class QuitDialog {
	
	private Activity act;
	
	public QuitDialog(final Activity act) {

		this.act = act;
		
	}
	
	public void exitConfirm() {
		AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(act.getParent());
		dialogBuilder.setTitle("提示"); //
		dialogBuilder.setMessage("您确定要退出吗？"); //
		dialogBuilder.setPositiveButton("取消", //
				new DialogInterface.OnClickListener() {//

					@Override
					public void onClick(DialogInterface dialog, int i) {
						
					}
				});
		dialogBuilder.setNegativeButton("确定",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int i) {
						ActivityTaskManager.getInstance().closeAllActivity();
						closeApplication();
					}
				});
		dialogBuilder.create().show();//
	}
	
	private void closeApplication(){
		String packName = act.getPackageName();  
	    ActivityManager activityMgr = (ActivityManager) act.getSystemService(Context.ACTIVITY_SERVICE);  
	    activityMgr.restartPackage(packName);//需要权限<uses-permission android:name="android.permission.RESTART_PACKAGES" />  
	    activityMgr.killBackgroundProcesses(packName);//需要权限 <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>  
	    android.os.Process.killProcess(android.os.Process.myPid());  
	}
}
